package zxh.com.until;

 

import java.io.File;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.web.multipart.MultipartFile;

import zxh.com.cons.Sys;
import zxh.com.model.FsFile;
 
 
 
public class UploadFile {

	 
     public static FsFile upfile( MultipartFile file,HttpServletRequest request ) {
    	 FsFile o=new FsFile();
    	 o.setFtype(file.getContentType());
    	 o.setOldname(file.getOriginalFilename());
    	 if(file.getSize()<1024){
    	     o.setSize(file.getSize()/1024+"bytes");
         }
    	 else if(file.getSize()/1024<1024){
    	     o.setSize(file.getSize()/1024+"KB");
         }
    	else{
    	    o.setSize(file.getSize()/1024/1024+"M");
         }
    	 System.out.println(o.getFtype());
    	  
    	 
    	 String pn=Sys.Upfile.parentPack+DateUtil.getNowTime2()+"/";
    	 
    	 
    	 
    	 String format=file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1,file.getOriginalFilename().length());
         String path = request.getSession().getServletContext().getRealPath(pn);
         
         o.setFormat(format);
         File fp=new File(path);
         if(!fp.exists()&&!fp.isDirectory()){  
        	 fp.mkdirs();  
         } 
         
         String fileName =UUID.randomUUID().toString()+"."+format;  
           
         File targetFile = new File(path, fileName);  
         
         try {  
             file.transferTo(targetFile);  
         } catch (Exception e) {  
             e.printStackTrace();  
         } 
         o.setNewname(fileName);
         o.setFpath(path+fileName);
        return   o  ;
     }
   
     public static void deleteFile(HttpServletRequest request,String path){   
		 String abPath = request.getSession().getServletContext().getRealPath("")+"/"+path;
		 File f=new File(abPath);
		 if(f.exists()&&f.isFile()){
		     f.delete();
         }
		 System.out.println("====删除："+abPath);
     } 
    
      
    
}

